﻿using AutoMapper;
using Customer.API.Applcation.Command.FinanceCommand;
using Customer.Domain;
using Customer.Infrstructre.FinanceManager;
using MediatR;

namespace Customer.API.Applcation.CommandHandlers.FinanceCommandHandler
{
    /// <summary>
    /// 修改应收管理中介者
    /// </summary>
    public class UpdReceivableCommandHandler : IRequestHandler<UpdReceivableCommand, int>
    {
        private readonly IReceivableRepository _receRep;
        private readonly IMapper mapper;

        public UpdReceivableCommandHandler(IReceivableRepository receRep, IMapper mapper)
        {
            _receRep = receRep;
            this.mapper = mapper;
        }

        public async Task<int> Handle(UpdReceivableCommand request, CancellationToken cancellationToken)
        {
            //var res = (await _receRep.GetAllAsync()).Where(x => x.ReceivableName == request.dto.ReceivableName);
            //if (res.Count() != 0)
            //{
            //    return 0;
            //}
            //else
            //{
                var res1 = (await _receRep.GetAllAsync()).FirstOrDefault(x => x.ReceivableId == request.dto.ReceivableId);
                return await _receRep.Update(mapper.Map(request.dto,res1));
            //}
        }
    }
}
